﻿<%@ Page Title="" Language="C#" MasterPageFile="~/InternalSite.Master" AutoEventWireup="true" CodeBehind="GroupMM.aspx.cs" Inherits="DATN.GroupCustomer.GroupMM" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent1" runat="server">

    <script type="text/javascript">
        //Reference of the GridView. 
        var TargetBaseControl = null;
        //Total no of checkboxes in a particular column inside the GridView.
        var CheckBoxes;
        //Total no of checked checkboxes in a particular column inside the GridView.
        var CheckedCheckBoxes;
        //Array of selected item's Ids.
        var SelectedItems;
        //Hidden field that wil contain string of selected item's Ids separated by '|'.
        var SelectedValues;

        window.onload = function () {
            //Get reference of the GridView. 
            try {
                TargetBaseControl = document.getElementById('<%= this.gvCheckboxes.ClientID %>');
            }
            catch (err) {
                TargetBaseControl = null;
            }

            //Get total no of checkboxes in a particular column inside the GridView.
            try {
                CheckBoxes = parseInt('<%= this.gvCheckboxes.Rows.Count %>');
            }
            catch (err) {
                CheckBoxes = 0;
            }

            //Get total no of checked checkboxes in a particular column inside the GridView.
            CheckedCheckBoxes = 0;

            //Get hidden field that wil contain string of selected item's Ids separated by '|'.
            SelectedValues = document.getElementById('<%= this.hdnFldSelectedValues.ClientID %>');

            //Get an array of selected item's Ids.
            if (SelectedValues.value == '')
                SelectedItems = new Array();
            else
                SelectedItems = SelectedValues.value.split('|');

            //Restore selected CheckBoxes' states.
            if (TargetBaseControl != null)
                RestoreState();
        }

        function HeaderClick(CheckBox) {
            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName('input');

            //Checked/Unchecked all the checkBoxes in side the GridView & modify selected items array.
            for (var n = 0; n < Inputs.length; ++n)
                if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf('chkBxSelect', 0) >= 0) {
                    Inputs[n].checked = CheckBox.checked;
                    if (CheckBox.checked)
                        SelectedItems.push(document.getElementById(Inputs[n].id.replace('chkBxSelect', 'hdnFldId')).value);
                    else
                        DeleteItem(document.getElementById(Inputs[n].id.replace('chkBxSelect', 'hdnFldId')).value);
                }

            //Update Selected Values. 
            SelectedValues.value = SelectedItems.join('|');

            //Reset Counter
            CheckedCheckBoxes = CheckBox.checked ? CheckBoxes : 0;
        }

        function ChildClick(CheckBox, HCheckBox, Id) {
            //Modifiy Counter;            
            if (CheckBox.checked && CheckedCheckBoxes < CheckBoxes)
                CheckedCheckBoxes++;
            else if (CheckedCheckBoxes > 0)
                CheckedCheckBoxes--;

            //Change state of the header CheckBox.
            if (CheckedCheckBoxes < CheckBoxes)
                HCheckBox.checked = false;
            else if (CheckedCheckBoxes == CheckBoxes)
                HCheckBox.checked = true;

            //Modify selected items array.
            if (CheckBox.checked)
                SelectedItems.push(Id);
            else
                DeleteItem(Id);

            //Update Selected Values. 
            SelectedValues.value = SelectedItems.join('|');
        }

        function RestoreState() {
            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName('input');

            //Header CheckBox
            var HCheckBox = null;

            //Restore previous state of the all checkBoxes in side the GridView.
            for (var n = 0; n < Inputs.length; ++n)
                if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf('chkBxSelect', 0) >= 0)
                    if (IsItemExists(document.getElementById(Inputs[n].id.replace('chkBxSelect', 'hdnFldId')).value) > -1) {
                        Inputs[n].checked = true;
                        CheckedCheckBoxes++;
                    }
                    else
                        Inputs[n].checked = false;
                else if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf('chkBxHeader', 0) >= 0)
                    HCheckBox = Inputs[n];

            //Change state of the header CheckBox.
            if (CheckedCheckBoxes < CheckBoxes)
                HCheckBox.checked = false;
            else if (CheckedCheckBoxes == CheckBoxes)
                HCheckBox.checked = true;
        }

        function DeleteItem(Text) {
            var n = IsItemExists(Text);
            if (n > -1)
                SelectedItems.splice(n, 1);
        }

        function IsItemExists(Text) {
            for (var n = 0; n < SelectedItems.length; ++n)
                if (SelectedItems[n] == Text)
                    return n;

            return -1;
        }     
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent2" runat="server">

<div id="contentx" class="clearingfix">
                    <center>
                            <br/>
                            <h3>Qu&#7843;n Lý Nhóm Khách Hàng:</h3>
                        <hr/>
                    </center>

              <div class="floatboxinternal">

            <!-----------------GridView Customer ------------------->
           <center>
            <asp:Label ID="Label1" runat="server" Text="Nhóm Khách Hàng"></asp:Label>
            <asp:DropDownList ID="Drr_Group" runat="server">
                  </asp:DropDownList>

                  <asp:Button ID="btnHienThi" runat="server" Text="Hi&#7875;n Th&#7883; Danh Sách" CssClass="bty"
                      onclick="btnHienThi_Click" />

                  <asp:Button ID="btnCreateNew" runat="server" Text="T&#7841;o M&#7899;i Nhóm" CssClass="bty"
                      onclick="btnCreateNew_Click" />

                  <br/>
                  <br/>
            
            
            <asp:GridView ID="gvCheckboxes" runat="server"
            AutoGenerateColumns="False" BackColor="White" 
            RowStyle-CssClass = "Grd_Row"
            OnRowDataBound="gvCheckboxes_RowDataBound" CellPadding="2" >

            <Columns>
                 <asp:TemplateField HeaderText="">
                     <ItemTemplate>
                        <asp:CheckBox ID="chkBxSelect" runat="server" />
                        <asp:HiddenField ID="hdnFldId" runat="server" Value='<%# Eval("Cus_Id") %>' />
                     </ItemTemplate>
                     <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                     <HeaderTemplate>
                        <asp:CheckBox ID="chkBxHeader" onclick="javascript:HeaderClick(this);" runat="server" />
                     </HeaderTemplate>
                  </asp:TemplateField>

             <asp:TemplateField>
                <HeaderTemplate>H&#7885; Và Tên </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lb_LastName" 
                                runat="server" 
                                Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>'></asp:Label>
                   <asp:Label ID="Lb_FirstName" 
                                runat="server" 
                                Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="140px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="140px" />
            </asp:TemplateField>
      
            <asp:BoundField DataField="Phone" HeaderText="&#272;i&#7879;n Tho&#7841;i"
                 NullDisplayText='<img src="../Content/images/error.png" />'>  
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
            </asp:BoundField>
            
            <asp:BoundField DataField="Mail" HeaderText="Th&#432; &#272;i&#7879;n T&#7917;"
                NullDisplayText='<img src="../Content/images/error.png" alt="c&#7853;p nh&#7853;t thêm thông tin"/>'>  
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
            </asp:BoundField>

            <asp:BoundField DataField="Habit" HeaderText="S&#7903; Thích"
                            NullDisplayText='<img src="../Content/images/error.png" />'> 
                            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="250px" />
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="250px" />
                            </asp:BoundField>

            <asp:BoundField DataField="Company" HeaderText="Công Ty"
                            NullDisplayText='<img src="../Content/images/error.png" alt="c&#7853;p nh&#7853;t thêm thông tin"/>'>  
                            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                            </asp:BoundField>

                     
                </Columns>
                <FooterStyle BackColor="#004a96" />
                <HeaderStyle BackColor="#004a96" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#004a96" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#b4cbd5" ForeColor="Black" Font-Size= "Small" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
    
    <asp:HiddenField ID="hdnFldSelectedValues" runat="server" />

                  <br/>
                  <center>
                  <asp:Button ID="btnDelete" runat="server" Text="Xóa Kh&#7887;i Nhóm" CssClass="bty" 
                          onclick="btnDelete_Click"/>

                  <asp:Button ID="btnMail" runat="server" Text="G&#7917;i Mail" CssClass="bty" 
                          onclick="btnMail_Click"/>

                  <br/>
                  <asp:Label ID="lb_AddtoGroup" runat="server" Text=""></asp:Label>
                  <br/>
                 

                  </center>

                  </center>
                  </div>
                  </div>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent3" runat="server">
</asp:Content>
